package com.example.video.video_server.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.video.common.exception.ServiceException;
import com.example.video.video_server.entity.VideoDramasSeries;
import com.example.video.video_server.entity.VideoInfo;
import com.example.video.video_server.entity.VideoResource;
import com.example.video.video_server.entity.vo.VideoDetailsVo;
import com.example.video.video_server.mapper.VideoInfoMapper;
import com.example.video.video_server.service.IVideoDramasSeriesService;
import com.example.video.video_server.service.IVideoInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.video.video_server.service.IVideoResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 视频基本信息表 服务实现类
 * </p>
 *
 * @author yj
 */
@Service
public class VideoInfoServiceImpl extends ServiceImpl<VideoInfoMapper, VideoInfo> implements IVideoInfoService {

    @Autowired
    private IVideoResourceService iVideoResourceService;

    @Autowired
    private IVideoDramasSeriesService iVideoDramasSeriesService;

    @Value("${reptilesTxUri}")
    private String reptilesTxUri;

    @Override
    public boolean del(Integer id) {
        List<VideoResource> list = iVideoResourceService.list(new QueryWrapper<>(new VideoResource().setVideoDraId(id)));
        if (list.size() > 0){
            throw new ServiceException("请先删除其资源");
        }
        boolean remove = this.removeById(id);
        return remove;
    }

    @Override
    public List getInfoByTxUrl(Integer videoDraId, String url) {
        List list = new ArrayList();
        try {
            ResponseEntity<String> res = new RestTemplate().exchange(reptilesTxUri+url, HttpMethod.GET, null, String.class);
            String data = JSONObject.parseObject(res.getBody()).getString("data");
            List<JSONObject> resList = JSONObject.parseObject(data, List.class);
            for (JSONObject s : resList){
                VideoResource videoResource = JSONObject.parseObject(s.toJSONString(), VideoResource.class);
                videoResource.setVideoDraId(videoDraId);
                list.add(videoResource);
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new ServiceException("获取资源路径失败");
        }
        return list;
    }

    @Override
    public VideoDetailsVo getDetailsById(Integer id) {
        VideoInfo byId = this.getById(id);
        if (byId == null){
            throw new ServiceException("该视频不存在");
        }
        List<VideoDramasSeries> videoDramasSeriesList = iVideoDramasSeriesService.list(new QueryWrapper<>(new VideoDramasSeries()
                .setVideoInfoId(id).setStatus("0")).orderByAsc("sort"));
        VideoDetailsVo videoDetailsVo = new VideoDetailsVo().setVideoInfo(byId).setVideoDramasSeries(videoDramasSeriesList);
        return videoDetailsVo;
    }

}
